package com.gsma.calendar.android.util;

import com.gsma.calendar.android.db.CalendarGsmaQuery;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class CalendarGsmaUtil {
	private Context context;

	public CalendarGsmaUtil(Context context) {
		this.context = context;
	}

	private int ListSelectedCalendars() {
		int result = 0;
		String[] projection = new String[] { "_id", "name" };
		String selection = "selected=1";
		String path = "calendars";

		Cursor managedCursor = getCalendarManagedCursor(projection, selection, path);

		if (managedCursor != null && managedCursor.moveToFirst()) {

			Log.i(Config.LOG_TAG, "Listing Selected Calendars Only");

			int nameColumn = managedCursor.getColumnIndex("name");
			int idColumn = managedCursor.getColumnIndex("_id");

			do {
				String calName = managedCursor.getString(nameColumn);
				String calId = managedCursor.getString(idColumn);
				Log.i(Config.LOG_TAG, "Found Calendar '" + calName + "' (ID=" + calId + ")");
				if (calName != null && calName.contains("Test")) {
					result = Integer.parseInt(calId);
				}
			} while (managedCursor.moveToNext());
		} else {
			Log.i(Config.LOG_TAG, "No Calendars");
		}

		return result;

	}

	private void ListAllCalendarDetails() {
		Cursor managedCursor = getCalendarManagedCursor(null, null, "calendars");

		if (managedCursor != null && managedCursor.moveToFirst()) {

			Log.i(Config.LOG_TAG, "Listing Calendars with Details");

			do {

				Log.i(Config.LOG_TAG, "**START Calendar Description**");

				for (int i = 0; i < managedCursor.getColumnCount(); i++) {
					Log.e(Config.LOG_TAG, managedCursor.getColumnName(i) + "=" + managedCursor.getString(i));
				}
				Log.i(Config.LOG_TAG, "**END Calendar Description**");
			} while (managedCursor.moveToNext());
		} else {
			Log.i(Config.LOG_TAG, "No Calendars");
		}

	}

	public void listAllCalendarEntries(int calID) {

		Cursor managedCursor = getCalendarManagedCursor(null, "calendar_id=" + calID, "events");

		if (managedCursor != null && managedCursor.moveToFirst()) {

			Log.i(Config.LOG_TAG, "Listing Calendar Event Details");

			do {

				Log.i(Config.LOG_TAG, "**START Calendar Event Description**");

				for (int i = 0; i < managedCursor.getColumnCount(); i++) {
					Log.i(Config.LOG_TAG, managedCursor.getColumnName(i) + "=" + managedCursor.getString(i));
				}
				Log.i(Config.LOG_TAG, "**END Calendar Event Description**");
			} while (managedCursor.moveToNext());
		} else {
			Log.i(Config.LOG_TAG, "No Calendars");
		}

	}

	private void ListCalendarEntry(int eventId) {
		Cursor managedCursor = getCalendarManagedCursor(null, null, "events/" + eventId);

		if (managedCursor != null && managedCursor.moveToFirst()) {

			Log.i(Config.LOG_TAG, "Listing Calendar Event Details");

			do {

				Log.i(Config.LOG_TAG, "**START Calendar Event Description**");

				for (int i = 0; i < managedCursor.getColumnCount(); i++) {
					Log.i(Config.LOG_TAG, managedCursor.getColumnName(i) + "=" + managedCursor.getString(i));
				}
				Log.i(Config.LOG_TAG, "**END Calendar Event Description**");
			} while (managedCursor.moveToNext());
		} else {
			Log.i(Config.LOG_TAG, "No Calendar Entry");
		}

	}

	private void ListCalendarEntrySummary(int eventId) {
		String[] projection = new String[] { "_id", "title", "dtstart" };
		Cursor managedCursor = getCalendarManagedCursor(projection, null, "events/" + eventId);

		if (managedCursor != null && managedCursor.moveToFirst()) {

			Log.i(Config.LOG_TAG, "Listing Calendar Event Details");

			do {

				Log.i(Config.LOG_TAG, "**START Calendar Event Description**");

				for (int i = 0; i < managedCursor.getColumnCount(); i++) {
					Log.i(Config.LOG_TAG, managedCursor.getColumnName(i) + "=" + managedCursor.getString(i));
				}
				Log.i(Config.LOG_TAG, "**END Calendar Event Description**");
			} while (managedCursor.moveToNext());
		} else {
			Log.i(Config.LOG_TAG, "No Calendar Entry");
		}

	}

	public Uri makeNewCalendarEntry(int calId, Cursor cursor) {
		ContentValues event = new ContentValues();

		event.put("calendar_id", calId);
		event.put("title", cursor.getString(CalendarGsmaQuery.SESSION));
		event.put("description", "All Day Event");
		event.put("eventLocation", cursor.getString(CalendarGsmaQuery.WHERE));

		event.put("dtstart", cursor.getLong(CalendarGsmaQuery.START_TIME));
		event.put("dtend", cursor.getLong(CalendarGsmaQuery.END_TIME));

		// event.put("allDay", 1); // 0 for false, 1 for true
		// event.put("eventStatus", 1);
		// event.put("visibility", 0);
		// event.put("transparency", 0);
		// event.put("hasAlarm", 0); // 0 for false, 1 for true

		Uri eventsUri = Uri.parse(getCalendarUriBase() + "events");
		Uri insertedUri = context.getContentResolver().insert(eventsUri, event);
		return insertedUri;
	}

	private Uri MakeNewCalendarEntry2(int calId) {
		ContentValues event = new ContentValues();

		event.put("calendar_id", calId);
		event.put("title", "Birthday [TEST]");
		event.put("description", "All Day Event");
		event.put("eventLocation", "Worldwide");

		long startTime = System.currentTimeMillis() + 1000 * 60 * 60 * 24;

		event.put("dtstart", startTime);
		event.put("dtend", startTime);

		event.put("allDay", 1); // 0 for false, 1 for true
		event.put("eventStatus", 1);
		event.put("visibility", 0);
		event.put("transparency", 0);
		event.put("hasAlarm", 0); // 0 for false, 1 for true

		Uri eventsUri = Uri.parse(getCalendarUriBase() + "events");
		Uri insertedUri = context.getContentResolver().insert(eventsUri, event);
		return insertedUri;

	}

	private int UpdateCalendarEntry(int entryID) {
		int iNumRowsUpdated = 0;

		ContentValues event = new ContentValues();

		event.put("title", "Changed Event Title");
		event.put("hasAlarm", 1); // 0 for false, 1 for true

		Uri eventsUri = Uri.parse(getCalendarUriBase() + "events");
		Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID);

		iNumRowsUpdated = context.getContentResolver().update(eventUri, event, null, null);

		Log.i(Config.LOG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");

		return iNumRowsUpdated;
	}

	private int DeleteCalendarEntry(int entryID) {
		int iNumRowsDeleted = 0;

		Uri eventsUri = Uri.parse(getCalendarUriBase() + "events");
		Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID);
		iNumRowsDeleted = context.getContentResolver().delete(eventUri, null, null);

		Log.i(Config.LOG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry.");

		return iNumRowsDeleted;
	}

	/**
	 * @param projection
	 * @param selection
	 * @param path
	 * @return
	 */
	public Cursor getCalendarManagedCursor(String[] projection, String selection, String path) {
		Uri calendars = Uri.parse("content://calendar/" + path);

		Cursor managedCursor = null;
		try {
			managedCursor = ((Activity) context).managedQuery(calendars, projection, selection, null, null);
		} catch (IllegalArgumentException e) {
			Log.w(Config.LOG_TAG, "Failed to get provider at [" + calendars.toString() + "]");
		}

		if (managedCursor == null) {
			// try again
			calendars = Uri.parse("content://com.android.calendar/" + path);
			try {
				managedCursor = ((Activity) context).managedQuery(calendars, projection, selection, null, null);
			} catch (IllegalArgumentException e) {
				Log.w(Config.LOG_TAG, "Failed to get provider at [" + calendars.toString() + "]");
			}
		}
		return managedCursor;
	}

	/*
	 * Determines if it's a pre 2.1 or a 2.2 calendar Uri, and returns the Uri
	 */
	private String getCalendarUriBase() {
		String calendarUriBase = null;
		Uri calendars = Uri.parse("content://calendar/calendars");
		Cursor managedCursor = null;
		try {
			managedCursor = ((Activity) context).managedQuery(calendars, null, null, null, null);
		} catch (Exception e) {
			// eat
		}

		if (managedCursor != null) {
			calendarUriBase = "content://calendar/";
		} else {
			calendars = Uri.parse("content://com.android.calendar/calendars");
			try {
				managedCursor = ((Activity) context).managedQuery(calendars, null, null, null, null);
			} catch (Exception e) {
				// eat
			}

			if (managedCursor != null) {
				calendarUriBase = "content://com.android.calendar/";
			}

		}

		return calendarUriBase;
	}

}
